package com.yueya.basic.role.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.jfinal.kit.JsonKit;
import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.yueya.basic.role.model.Role;
import com.yueya.basic.role.model.RoleMenu;
import com.yueya.basic.role.model.RoleUser;
import com.yueya.common.action.BaseController;
import com.yueya.common.model.Json;
import com.yueya.common.model.Tree;

public class RoleController extends BaseController {

	Map<String, Object> map = new HashMap<String, Object>();

	public void index() {
		render("/WEB-INF/views/basic/role/role_list.jsp");
	}

	public void getListPager() {
		map.clear();

		map.put("delflag", "0");
		if (this.getPara("name") != null && !"".equals(this.getPara("name"))) {
			map.put("name", getPara("name").trim());
		}

		Page<Record> page = Role.service.pagination(this.getPage(),
				this.getRows(), map);

		jsonMap.put("total", page.getTotalRow());
		jsonMap.put("rows", page.getList());
		renderJson(JsonKit.toJson(jsonMap));
	}

	public void save() {
		Role role = getModel(Role.class);
		boolean bl = Role.service.save(role);
		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("新增用户成功");
		} else {
			j.setSuccess(false);
			j.setMsg("新增用户失败");
		}
		this.renderJson(j);
	}

	public void doAdd() {
		render("/WEB-INF/views/basic/role/role_edit.jsp");
	}

	public void doEdit() {
		String id = this.getPara("id");
		Role role = Role.dao.findById(id);
		this.setAttr("role", role);
		render("/WEB-INF/views/basic/role/role_edit.jsp");
	}

	public void removeRole() {
		boolean bl = false;
		if (getPara("id") != null) {
			String id = this.getPara("id").replace("/", "").trim();
			bl = Role.service.removeRole(id);
		}
		Json j = new Json();
		if (bl) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	public void bathremove() {
		Boolean bl = false;
		String ids = this.getPara("ids");
		if (ids != null) {
			bl = Role.service.bathremove(ids);
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("删除成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("删除失败!");
		}
		this.renderJson(j);
	}

	public void userset() {
		this.setAttr("id", getPara("id"));
		render("/WEB-INF/views/basic/role/role_user.jsp");
	}

	public void menuset() {
		this.setAttr("id", this.getPara("id"));
		render("/WEB-INF/views/basic/role/role_menu.jsp");
	}

	public void toTreeRoleMenu() {
		String id = getPara("id");
		List<Tree> treeList = RoleMenu.service.getTreeRoleMenu(id);
		renderJson(JsonKit.toJson(treeList));
	}

	public void toTreeRoleUser() {
		String id = getPara("id");
		List<Tree> treeList = RoleUser.service.getTreeRoleUser(id);
		renderJson(JsonKit.toJson(treeList));
	}

	public void saveRoleMenu() {
		boolean bl = false;

		String roleid = this.getPara("roleid");
		String menuids = this.getPara("menus");

		if (StrKit.notBlank(roleid) && StrKit.notBlank(menuids)) {
			bl = RoleMenu.service.saveRoleMenus(roleid, menuids.split(","));
		}

		Json j = new Json();
		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("设置成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("设置失败!");
		}
		this.renderJson(j);
	}

	public void saveRoleUser() {
		boolean bl = false;

		String roleid = getPara("roleid");
		String userids = getPara("ids");

		if (StrKit.notBlank(roleid) && StrKit.notBlank(userids)) {
			bl = RoleUser.service.saveRoleUsers(roleid, userids.split(","));
		}

		Json j = new Json();

		if (bl == true) {
			j.setSuccess(true);
			j.setMsg("设置成功!");
		} else {
			j.setSuccess(false);
			j.setMsg("设置失败!");
		}
		this.renderJson(j);
		renderJson(j);
	}
}
